package com.hairy.nipples;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.hairy.nipples.sound.BeepService;
import com.hairy.nipples.util.Logger;

public class Settings extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		final TextView reading = (TextView) findViewById(R.id.interval_reading);
		reading.setText("Interval " + BeepService.RUN_AFTER_HOURS + " hours");

		ToggleButton onOffButton = (ToggleButton) findViewById(R.id.toggleButton1);
		onOffButton.setChecked(BeepService.ENABLED);
		final Intent beepService = new Intent(this, BeepService.class);
		onOffButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ToggleButton button = (ToggleButton) v;
				if (button.isChecked()) {
					startService(beepService);
					Logger.debug("startservice");
					BeepService.ENABLED = true;
				} else {
					Logger.debug("stopservice");
					BeepService.ENABLED = false;
				}
			}
		});

		SeekBar mSeekBar = (SeekBar) findViewById(R.id.seekBar1);
		int progress = (BeepService.RUN_AFTER_HOURS - 12) * 100 / 12;
		mSeekBar.setProgress(progress);
		mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				Logger.debug("progress changes " + progress);

				int calculatedRating = (int) progress * 12 / 100 + 12;
				reading.setText("After " + calculatedRating + " hours");
				BeepService.RUN_AFTER_HOURS = calculatedRating;
			}
		});
	}
}
